<!--车辆管理页-->
<template>
  <div style="height: 6vh;padding: 10px 20px;background-color: #fff">
    <span style="line-height: 60px;font-size: 20px">车辆管理</span>
    <el-button type="primary" style="float: right;margin-top: 10px" @click="beforeAddVehicle">新增车辆</el-button>
  </div>
  <!--  新建车辆弹窗-->
  <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      style="width: 1000px;padding: 40px"
      :before-close="handleClose"
  >
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="车辆品牌">
            <el-input placeholder="请输入内容"
                      v-model="saveVehicleForm.brand"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆牌号">
            <el-input v-model="saveVehicleForm.license" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="车辆型号">
            <el-input v-model="saveVehicleForm.model" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆识别码">
            <el-input v-model="saveVehicleForm.code" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="车辆排量">
            <el-select v-model="saveVehicleForm.displacement" placeholder="请选择" @change="loadLeader">
              <el-option label="1.6" value="1"></el-option>
              <el-option label="2.5" value="2"></el-option>
              <el-option label="4" value="3"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆类型">
            <el-select v-model="saveVehicleForm.type" placeholder="请选择">
              <el-option v-for="item in vehicleTypeArr" :label="item.label" :value="item.value"></el-option>
<!--              <el-option label="轿车" value="10"></el-option>-->
<!--              <el-option label="货车" value="20"></el-option>-->
<!--              <el-option label="客车" value="30"></el-option>-->
<!--              <el-option label="挂车" value="40"></el-option>-->
            </el-select>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="车身颜色">
            <el-select v-model="saveVehicleForm.color" placeholder="请选择" @change="loadLeader">
              <el-option label="黑" value="10"></el-option>
              <el-option label="白" value="20"></el-option>
              <el-option label="蓝" value="30"></el-option>
              <el-option label="灰" value="40"></el-option>
              <el-option label="银" value="50"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="里程数">
            <el-input v-model="saveVehicleForm.kilometers" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row :gutter="20" @change="validateDate">
        <el-col :span="12">
          <el-form-item label="购买时间">
            <el-date-picker
                type="date"
                v-model="saveVehicleForm.buyTime"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                 @change="validateDate"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上牌时间">
            <el-date-picker
                type="date"
                v-model="saveVehicleForm.regTime"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
                 @change="validateDate"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="购买价格">
            <el-input v-model="saveVehicleForm.price" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电池类型">
            <el-select v-model="saveVehicleForm.batteryType" placeholder="请选择">
              <el-option v-for="item in batteryTypeArr" :label="item.label" :value="item.value"></el-option>
<!--              <el-option label="铅酸蓄电池" value="10"></el-option>-->
<!--              <el-option label="镍氢电池" value="20"></el-option>-->
<!--              <el-option label="钠硫电池" value="30"></el-option>-->
<!--              <el-option label="二次锂电池" value="40"></el-option>-->
<!--              <el-option label="空气电池" value="50"></el-option>-->
<!--              <el-option label="三元锂电池" value="60"></el-option>-->
<!--              <el-option label="碱性燃料电池" value="70"></el-option>-->
            </el-select>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveVehicle">确定</el-button>
    </template>

  </el-dialog>

  <!--  车辆搜索卡片-->
  <el-card style="margin: 20px;height: 70px">
    <el-form :inline="true">
      <el-form-item label="车辆品牌" style="width: 220px;">
        <el-input placeholder="请输入内容" v-model="searchVehicleForm.brand" @keydown.enter="loadVehicle"></el-input>
      </el-form-item>
      <el-form-item label="车辆号" style="width: 220px;">
        <el-input placeholder="请输入内容" v-model="searchVehicleForm.license" @keydown.enter="loadVehicle"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadVehicle">查询</el-button>
      </el-form-item>
    </el-form>

  </el-card>
  <!--  车辆数据表格卡片-->
  <el-card style="margin: 20px;">
    <el-table :data="vehicleArr">
      <el-table-column label="编号" type="index" width="80" align="center"></el-table-column>
      <el-table-column label="车辆品牌" prop="brand" align="center"></el-table-column>
      <el-table-column label="车牌号" prop="license" align="center"></el-table-column>
      <el-table-column label="车辆识别码" prop="code" align="center"></el-table-column>
      <el-table-column label="车辆类型" prop="type" align="center"
                       :formatter="vehicleTypeFormatter"></el-table-column>
      <el-table-column label="购买价格" prop="price" align="center"></el-table-column>
      <el-table-column label="购买时间" prop="buyTime" align="center"></el-table-column>
      <el-table-column label="上牌时间" prop="regTime" align="center"></el-table-column>
      <el-table-column label="电池类型" prop="batteryType" align="center"
                       :formatter="batteryTypeFormatter"></el-table-column>
      <el-table-column label="车辆状态" prop="status" align="center"
                       :formatter="vehicleStatusFormatter"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <!--          <el-button link type="primary" :disabled="scope.row.level==40"-->
          <!--                     @click="resetPassword(scope.row.id)">密码重置-->
          <!--          </el-button>-->
                    <el-button link type="primary"
                               @click="editVehicle(scope.row.id)">编辑
                    </el-button>
                    <el-button link type="primary"
                               @click="deleteVehicle(scope.row.id)">删除
                    </el-button>
        </template>
      </el-table-column>
    </el-table>

  </el-card>

</template>
<script setup>

import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";

//定义变量控制弹窗是否出现
const dialogVisible = ref(false);
//定义变量控制弹窗的标题
const dialogTitle = ref('新增车辆');
//点击"新建车辆"按钮,让弹窗出现且改标题
const beforeAddVehicle = () => {
  dialogTitle.value = '新增车辆';
  dialogVisible.value = true;

}

//定义对象用来保存表单的车辆数据
const saveVehicleForm = ref({
  brand: '',
  license: '',
  model: '',
  code: '',
  displacement: '',
  type: '',
  color: '',
  kilometers: '',
  buyTime: '',
  regTime: '',
  price: '',
  batteryType: '',
});

//定义校验上牌时间与购买时间的先后顺序
const validateDate=()=>{
  const buyTime = saveVehicleForm.value.buyTime;
  const regTime = saveVehicleForm.value.regTime;
  if (buyTime && regTime && new Date(buyTime) > new Date(regTime)) {
      ElMessage.error('上牌时间不能早于购买时间');
      return false;
  }
}
//定义保存车辆数据的方法
const saveVehicle = () => {
  let data = qs.stringify(saveVehicleForm.value);
  console.log(data);
  //发送请求保存车辆数据
  axios.post(BASE_URL + '/v1/vehicle/save', data)
      .then((response) => {
        if (response.data.code === 2000) {
          ElMessage.success('保存成功');
          //关闭弹窗并清空弹窗数据
          dialogVisible.value = false;
          saveVehicleForm.value = {};
          //重新加载车辆数据
          loadVehicle();
        } else {
          //保存失败
          ElMessage.error("车牌号或车辆识别码不允许重复！");
        }
      })
}
//定义关闭弹窗的方法
const handleClose = () => {
  if (confirm("确定关闭弹窗吗?")) {
    dialogVisible.value = false;
    saveVehicleForm.value = {};
  }
}
//定义对象用来保存搜索卡片的用户搜索条件
const searchVehicleForm = ref({
  brand: '',
  license: ''
});
//定义数组用来保存用户表格数据
const vehicleArr = ref([]);
//定义查询用户数据的方法
const loadVehicle = () => {
  let data = qs.stringify(searchVehicleForm.value);
  console.log(data);
  //注意:有条件根据搜索条件查,没有条件默认查询所有的用户,所以是以 ? 拼接的方式传参的,是get请求
  axios.get(BASE_URL + '/v1/vehicle/select?' + data)
      .then((response) => {
        if (response.data.code === 2000) {
          vehicleArr.value = response.data.data;
          console.log(vehicleArr.value);
        } else {
          ElMessage.error(response.data.msg);
        }
      })
}

//第1步：修改上方的绑定，删除写死的选项值
//第2步：定义三个响应式变量用来保存对应的字典集合数据 DictOptionVOList
const vehicleTypeArr = ref([]);
const batteryTypeArr = ref([]);


//页面一加载完毕,立即调用加载用户的方法
onMounted(() => {
  loadVehicle();//加载车辆数据
  //加载车辆类型字典数据
  loadDictOptions(vehicleTypeArr, 'vehicle_type');
  //加载电池类型字典数据
  loadDictOptions(batteryTypeArr, 'battery_type');
})
//定义重置搜索条件的方法
const resetSearch = () => {
  //清空搜索条件
  searchVehicleForm.value = {};
  //重新加载所有用户数据
  loadVehicle();
}

//定义一个车辆类型转换的方法
const vehicleTypeFormatter = (row, column, cellValue, index) => {
  if(!vehicleTypeArr.value){
    return cellValue;//字典集合数据还没有加载完毕,直接返回原值
  }
  //遍历车辆类型字典集合,找到对应的车辆类型名称
  for(let item of vehicleTypeArr.value){
    if(item.value==cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
  // if (cellValue == 10) {
  //   return '轿车';
  // } else if (cellValue == 20) {
  //   return '货车';
  // } else if (cellValue == 30) {
  //   return '客车';
  // } else if (cellValue == 40) {
  //   return '挂车';
  // }
}
//定义一个电池类型转换的方法
const batteryTypeFormatter = (row, column, cellValue, index) => {
  // if (cellValue == 10) {
  //   return '铅酸蓄电池';
  // } else if (cellValue == 20) {
  //   return '镍氢电池';
  // } else if (cellValue == 30) {
  //   return '钠硫电池';
  // } else if (cellValue == 40) {
  //   return '二次锂电池';
  // } else if (cellValue == 50) {
  //   return '空气电池';
  // } else if (cellValue == 60) {
  //   return '三元锂电池';
  // } else if (cellValue == 70) {
  //   return '碱性燃料电池';
  // }
  if(!batteryTypeArr.value){
    return cellValue;//字典集合数据还没有加载完毕,直接返回原值
  }
  //遍历电池类型字典集合,找到对应的电池类型名称
  for(let item of batteryTypeArr.value){
    if(item.value==cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}
//定义一个车辆状态转换的方法
const vehicleStatusFormatter = (row, column, cellValue, index) => {
  if (cellValue == 1) {
    return '空闲';
  } else if (cellValue == 2) {
    return '占用';
  }
}


//点击用户表格的“编辑”按钮
const editVehicle = (vehicleId) => {
  dialogVisible.value = true;
  dialogTitle.value = '编辑车辆';
  //根据当前行的用户id查询该用户数据的详情信息，回显在弹窗表单中
  axios.get(BASE_URL + '/v1/vehicle/select?id=' + vehicleId)
      .then((response) => {
        if (response.data.code == 2000) {
          //注意:后端返回的是List<VehicleV0>,所以查一条数据的话拿的是集合的第一个元素data[0]
          saveVehicleForm.value = response.data.data[0];

        } else {
          ElMessage.error(response.data.msg);
        }
      })
}

//定义方法删除车辆
const deleteVehicle = (vehicleId) => {
  if (confirm("确定删除该车辆吗？")) {
    axios.post(BASE_URL + '/v1/vehicle/deleteVehicle/' + vehicleId)
        .then((response) => {
          if (response.data.code == 2000) {
            ElMessage.success('删除成功');
            //删除成功后重新加载车辆数据
            loadVehicle();
          } else {
            ElMessage.error(response.data.msg);
          }
        })
  }
}

</script>
<style scoped>

</style>














